/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class MenuSettingsForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int OK_COMMAND_ID = 1;
    
    private static final int VOLUME_ITEM_ID = 0;
    private static final int VIBRATION_ITEM_ID = 1;
    private static final int CENTER_BUTTON_SETTINGS_ITEM_ID = 2;
    private static final int THEMES_ITEM_ID = 3;
    private static final int FONT_EDIT_ITEM_ID = 4;
    
    private MenuList items;
    private CellItem volumeItem;
    private CellItem centerButtonSettingsItem;
    
    private VolumeSettingsForm volumeSettingsForm;
    private VibrationSettingsForm vibrationSettingsForm;
    private CenterButtonSettingsMenuForm centerButtonSettingsMenuForm;
    private ThemeEditForm themeEditForm;
    private FontEditForm fontEditForm;
    
    public MenuSettingsForm()
    {
        super("settings");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        items = new MenuList();
        volumeItem = new CellItem(VOLUME_ITEM_ID, "volume");
        items.addItem(volumeItem);
        items.addItem(new CellItem(VIBRATION_ITEM_ID, "vibration"));
        centerButtonSettingsItem = new CellItem(CENTER_BUTTON_SETTINGS_ITEM_ID, "center_butt_set");
        items.addItem(centerButtonSettingsItem);
        items.addItem(new CellItem(THEMES_ITEM_ID ,"themes"));
        items.addItem(new CellItem(FONT_EDIT_ITEM_ID ,"font_edit"));
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void itemSelected(int id)
    {
        switch(id)
        {
            case VOLUME_ITEM_ID :  showVolumeSettingsForm(); break;
            case VIBRATION_ITEM_ID : showVibrationSettingsForm(); break;
            case CENTER_BUTTON_SETTINGS_ITEM_ID : showCenterButtonSettingsMenuForm(); break;
            case THEMES_ITEM_ID : showThemeEditForm(); break;
            case FONT_EDIT_ITEM_ID : showFontEditForm(); break;
        }
    }
    
    public void show()
    {
        //Устанавливаем активные\неактивные пункты
        setItemsState();
        
        super.show();
    }
    
    private void setItemsState()
    {
        volumeItem.setEnabled(state == CONNECTED_STATE);
        centerButtonSettingsItem.setEnabled(state == CONNECTED_STATE);
    }
    
    private void showVolumeSettingsForm()
    {
        if (volumeSettingsForm == null)
        {
            volumeSettingsForm = new VolumeSettingsForm();
        }
        
        ScreenEngine.getEngine().push(volumeSettingsForm);
    }
    
    private void showVibrationSettingsForm()
    {
        if (vibrationSettingsForm == null)
        {
            vibrationSettingsForm = new VibrationSettingsForm();
        }
        
        ScreenEngine.getEngine().push(vibrationSettingsForm);
    }
    
    private void showCenterButtonSettingsMenuForm()
    {
        if (centerButtonSettingsMenuForm == null)
        {
            centerButtonSettingsMenuForm = new CenterButtonSettingsMenuForm();
        }
        
        ScreenEngine.getEngine().push(centerButtonSettingsMenuForm);
    }
    
    private void showThemeEditForm()
    {
        if (themeEditForm == null)
        {
            themeEditForm = new ThemeEditForm();
        }
        
        ScreenEngine.getEngine().push(themeEditForm);
    }
    
    private void showFontEditForm()
    {
        if (fontEditForm == null)
        {
            fontEditForm = new FontEditForm();
        }
        
        ScreenEngine.getEngine().push(fontEditForm);
    }
}
